#include "sys.h"

extern TIM_ICUserValueTypeDef  TIM_ICUserValueStructure2, TIM_ICUserValueStructure5;


void Trig_GPIO_Config(void)
{
		GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;	
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_Init(GPIOA, &GPIO_InitStructure);	
	  GPIO_ResetBits(GPIOA, GPIO_Pin_5|GPIO_Pin_6);	
}



//TIM2,TIM5的arr是32位
 void HCSR04_Config(u32 arr,u16 psc)
 {
	 Trig_GPIO_Config();
	 
	 GPIO_InitTypeDef GPIO_InitStructure;
	 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	 TIM_ICInitTypeDef  TIM_ICInitStructure;
	 NVIC_InitTypeDef NVIC_InitStructure;
	 
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM5, ENABLE); 
	 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 	//使能PORTA时钟	
	 
	 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3
	 NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;		//子优先级3
	 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	 NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器、
	 NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
	 NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器、
	 
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIOA0 for TIM5 GPIOA1 for TIM2 
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	//速度100MHz
	 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
	 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
	 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0 PA1
	 
	 GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0复用位定时器5
	 GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2); //PA1复用位定时器2
	 
   TIM_TimeBaseStructure.TIM_Prescaler=psc;  //定时器分频
	 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	 TIM_TimeBaseStructure.TIM_Period=arr;   //自动重装载值
	 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
	 TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
	 
	 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
   TIM_ICInitStructure.TIM_ICFilter = 0x00;
   TIM_ICInit(TIM5, &TIM_ICInitStructure);
	 
	 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
   TIM_ICInitStructure.TIM_ICFilter = 0x00;
   TIM_ICInit(TIM2, &TIM_ICInitStructure);
	 
	 TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC2,ENABLE);//允许更新中断 ,允许TIM2 CC2IE捕获中断
	 TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许TIM5 CC1IE捕获中断
	 
	 TIM_Cmd(TIM2,ENABLE ); 	//使能定时器2
   TIM_Cmd(TIM5,ENABLE ); 	//使能定时器5
 }

 
void TIM2_IRQHandler(void)
{
//	printf("TIM2_IRQ\r\n");
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)//有可能一次测量时间过长导致定时器溢出
	{
		TIM_ICUserValueStructure2.Capture_Period++;
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);	
	}
		// 捕获中断
	if ( TIM_GetITStatus (TIM2,TIM_IT_CC2) != RESET)
	{
		// 第一次捕获
		if (TIM_ICUserValueStructure2.Capture_StartFlag == 0 )
		{
			// 计数器清0
			TIM_SetCounter(TIM2,0);
			// 自动重装载寄存器更新标志清0
			TIM_ICUserValueStructure2.Capture_Period = 0;
      // 存捕获比较寄存器的值的变量的值清0			
			TIM_ICUserValueStructure2.Capture_CcrValue = 0;

			// 当第一次捕获到上升沿之后，就把捕获边沿配置为下降沿
			TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Falling);
      // 开始捕获标准置1			
			TIM_ICUserValueStructure2.Capture_StartFlag = 1;			
		}
		// 下降沿捕获中断
		else // 第二次捕获
		{
			// 获取捕获比较寄存器的值，这个值就是捕获到的高电平的时间的值
			TIM_ICUserValueStructure2.Capture_CcrValue =TIM_GetCapture2(TIM2);

			// 当第二次捕获之后，就把捕获边沿配置为上升沿，好开启新的一轮捕获
			TIM_OC2PolarityConfig(TIM2, TIM_ICPolarity_Rising);
      // 开始捕获标志清0		
			TIM_ICUserValueStructure2.Capture_StartFlag = 0;
      // 捕获完成标志置1			
			TIM_ICUserValueStructure2.Capture_FinishFlag = 1;		
		}
		
		if(TIM_ICUserValueStructure2.Capture_FinishFlag==1)
		{
			uint32_t time;
			static uint32_t cout2=0;
			float a;
			static float d[10]={0}; //均值滤波数组
			static int i = 0;
			time=TIM_ICUserValueStructure2.Capture_Period*4294967295+TIM_ICUserValueStructure2.Capture_CcrValue+1;
			a=time/1000.0000;
			//printf ( "\r\n测得高电平脉宽时间：%f s\r\n",a);
			d[i++] = (346*a)/2;
			if(i == 10)
			{
				i=0;
				d_right = 0;
				for(int j=0; j<10; j++)
				{
					d_right+=d[j];
				}
				d_right /= 10;
				//				u5_printf ( "\r\n测得右边距离:%f \r\n",d_right);
			}
//			if(!(cout2%10))u5_printf ( "\r\n测得右边距离：%f \r\n",d_right);
			TIM_ICUserValueStructure2.Capture_FinishFlag = 0;	//每捕获1次（测量一次），就计算一次	
			cout2++;
		}
		

		TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);	 
	}

} 
 

void TIM5_IRQHandler(void)
{
//	printf("TIM5_IRQ\r\n");
	if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)//有可能一次测量时间过长导致定时器溢出
	{
		TIM_ICUserValueStructure5.Capture_Period++;
		TIM_ClearITPendingBit(TIM5,TIM_IT_Update);	
	}
		// 捕获中断
	if ( TIM_GetITStatus (TIM5,TIM_IT_CC1) != RESET)
	{
		// 第一次捕获
		if (TIM_ICUserValueStructure5.Capture_StartFlag == 0 )
		{
			// 计数器清0
			TIM_SetCounter(TIM5,0);
			// 自动重装载寄存器更新标志清0
			TIM_ICUserValueStructure5.Capture_Period = 0;
      // 存捕获比较寄存器的值的变量的值清0			
			TIM_ICUserValueStructure5.Capture_CcrValue = 0;

			// 当第一次捕获到上升沿之后，就把捕获边沿配置为下降沿
			TIM_OC1PolarityConfig(TIM5, TIM_ICPolarity_Falling);
      // 开始捕获标准置1			
			TIM_ICUserValueStructure5.Capture_StartFlag = 1;			
		}
		// 下降沿捕获中断
		else // 第二次捕获
		{
			// 获取捕获比较寄存器的值，这个值就是捕获到的高电平的时间的值
			TIM_ICUserValueStructure5.Capture_CcrValue =TIM_GetCapture1(TIM5);

			// 当第二次捕获之后，就把捕获边沿配置为上升沿，好开启新的一轮捕获
			TIM_OC1PolarityConfig(TIM5, TIM_ICPolarity_Rising);
      // 开始捕获标志清0		
			TIM_ICUserValueStructure5.Capture_StartFlag = 0;
      // 捕获完成标志置1			
			TIM_ICUserValueStructure5.Capture_FinishFlag = 1;		
		}
		
		if(TIM_ICUserValueStructure5.Capture_FinishFlag==1)
		{
			uint32_t time;
			static uint32_t cout5=0;
			float a;
			static float d[10]={0}; //均值滤波数组
			static int i = 0;
			time=TIM_ICUserValueStructure5.Capture_Period*4294967295+TIM_ICUserValueStructure5.Capture_CcrValue+1;
			a=time/1000.0000;
			//printf ( "\r\n测得高电平脉宽时间：%f s\r\n",a);
			d[i++] = (346*a)/2;
			if(i == 10)
			{
				i=0;
				d_left = 0;
				for(int j=0; j<10; j++)
				{
					d_left+=d[j];
				}
				d_left /= 10;
//				u5_printf ( "\r\n测得左边距离:%f \r\n",d_left);
			}
//			if(!(cout5%10))u5_printf ( "\r\n测得左边距离：%f \r\n",d_left);
			TIM_ICUserValueStructure5.Capture_FinishFlag = 0;	//每捕获1次（测量一次），就计算一次	
			cout5++;
		}
		

		TIM_ClearITPendingBit(TIM5,TIM_IT_CC1);	 
	}

}





